<?

include_once('../includes/loader.php');
$debug = isset($_GET['debug']);
if (isset($_GET['device']) && !empty($_GET['device']))  {
	$device = new objDevice($_GET['device']);
	$device->os->getResources();
	$path = $device->getGraphPath($_GET);
	$device->graph($_GET);
	if (is_file($path)) {
		header('Content-type: image/png');
		$fd = fopen($path,'r');
		fpassthru($fd);
		fclose($fd);	
	} else if ($debug == true) {
	} else {
		$width = isset($_GET['width'])?$_GET['width']:20;
		$height = isset($_GET['height'])?$_GET['height']:20;
		generateMsg('No Data',$width,$height);
	}
} else if (isset($_GET['map'])) {
	$cfg= array('INCLUDE ../config/map.conf');
	$sql = "SELECT * FROM devices WHERE map !='';";
	foreach ($nms->db->getObjects($sql) AS $d) {
		$device = new objDevice($d->id);
		$cfg[] = 'NODE device'.$device->id;
		$cfg[] = ' LABEL '.$device->description;
		$cfg[] = ' ICON img/device/'.$device->os->getType().'.png';
		$lines = explode(chr(10),$d->map);
		foreach ($lines AS $line) {
			$cfg[] = ' '.$line;
		}
	}

	$sql = "SELECT * FROM interfaces WHERE map!=''";
	foreach ($nms->db->getObjects($sql) AS $i) {
		$device = new objDevice($i->device_id);
		$device->os->getResources();
		$interface = $device->interfaces[$i->ifIndex];
		$cfg[] = 'LINK device'.$i->device_id.'port'.$i->ifIndex;
		$cfg[] = ' TARGET '.$interface->getRrdPath().':ifInOctets:ifOutOctets';
		$cfg[] = ' BANDWIDTH '.strtoupper(str_replace(' ','',$interface->getBandwidth()));
		$lines = explode(chr(10),$i->map);
		foreach ($lines AS $line) {
			$cfg[] = ' '.$line;
		}
	}
	
	$map = new WeatherMap;
	header('Content-type: image/png');
	$map->ReadConfig($cfg);
	$map->ReadData();
	$map->DrawMap('','',250,TRUE,FALSE,FALSE);
	exit();
}


function generateMsg($msg='Unknown Error',$width=400,$height=200) {
	header('Content-type: image/png');
	$string = "No Data";
	$im     = imagecreate($width, $height);
	$orange = imagecolorallocate($im, 255, 255, 255);
	$px     = (imagesx($im) - 7.5 * strlen($msg)) / 2;
	imagestring($im, 3, $px, $height / 2 - 8, $msg, imagecolorallocate($im, 100, 100, 100));
	imagepng($im);
	imagedestroy($im);
	exit();}